/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui.tableview;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.UIManager;
import todomanager.model.Task;
import todomanager.ui.ActionControl;
import todomanager.ui.TaskDisplay;

/**
 * Extends {@code TaskDisplay} to transform it into a suitable table
 * cell component.
 * 
 * @author
 * Nosheen Zaza
 */
public class TaskDisplayCellComponent extends TaskDisplay {

    protected ResourceBundle settings;

    /**
     * Create and initialize an instance.
     */
    public TaskDisplayCellComponent() {

        /*
         * Switch between compact and details modes, when clicking on the
         * text field.
         */
        checkTextField.getTextfield().addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (compactMode) {
                    detailsPanel.setVisible(false);
                    morePanel.setVisible(true);
                    compactMode = false;
                } else {
                    detailsPanel.setVisible(true);
                    morePanel.setVisible(false);
                    compactMode = true;
                    
                    /* Update the progress and date labels immediately, even if
                     * table editing is note done.
                     */
                    progressLabel.setText("Progress: "
                            + getProgressMeter().getProgressBar().getValue()
                            + "0%");

                    if (dateChooser.getDateEditor().getDate() != null) {
                        dateLabel.setText(settings.getString("Due")
                                + ": "
                                + dateChooser.getCalendar().get(GregorianCalendar.DATE)
                                + "-"
                                + (dateChooser.getCalendar().get(GregorianCalendar.MONTH) + 1)
                                + "-"
                                + dateChooser.getCalendar().get(GregorianCalendar.YEAR));
                    } else
                        dateLabel.setText("");
                }
            }
        });
    }

    /**
     * Updates the data of this component.
     * @param t A task.
     * @param isSelected
     */
    public void updateData(Task t, boolean isSelected) {
        settings = ActionControl.getInstance().getStringsBundle();
        
        this.getCheckTextField().getTextfield().setText(t.getText());
        this.getCheckTextField().getCheckBox().setSelected(t.isDone());
        this.getProgressMeter().getProgressBar().setValue(t.getProgress());

        this.progressLabel.setText(settings.getString("Progress") + ": "
                + this.getProgressMeter().getProgressBar().getValue()
                + "0%");
        this.dateChooser.setCalendar(null);
        this.dateLabel.setText("");

        if (t.getDueDate() != null) {
            GregorianCalendar currentDate = new GregorianCalendar();

            // If the task is overdue and not done, display it in red
            if (t.getDueDate().before(currentDate)
                    && !(t.getDueDate().get(
                    GregorianCalendar.DAY_OF_YEAR)
                    == currentDate.get(GregorianCalendar.DAY_OF_YEAR))
                    && (!t.isDone())) {
                this.checkTextField.getTextfield().setForeground(
                        UIManager.getDefaults().getColor("Color.Red"));
            }
            else {
                this.checkTextField.getTextfield().setForeground(
                        UIManager.getDefaults().getColor("Color.Black"));
            }
            dateChooser.setCalendar(t.getDueDate());
            dateLabel.setText(settings.getString("Due") + ": "
                    + t.getDueDate().get(GregorianCalendar.DATE) + "-"
                    + (t.getDueDate().get(GregorianCalendar.MONTH) + 1) + "-"
                    + t.getDueDate().get(GregorianCalendar.YEAR));
            if (isSelected) {
                TaskDisplayCellComponent.this.setBorder(
                        BorderFactory.createLineBorder(
                        UIManager.getDefaults().getColor("Color.Selected-cell"), 2));
            } else {
                TaskDisplayCellComponent.this.setBorder(
                        BorderFactory.createEmptyBorder());
            }
        } else {
            this.checkTextField.getTextfield().setForeground(
                        UIManager.getDefaults().getColor("Color.Black"));
        }
    }
}
